/*jquery.accordion-1.0.0.js*/

(function ($) {
    var autoSize = {
        none: 0,
        fill: 1,
        limit: 2
    };

    var methods = {
        init: function (options) {
            return this.each(function () {
                var data = $(this).data("accordion-data");

                if (!data)
                    $(this).data("accordion-data", {
                        options: options,
                        currentSelectedIndex: options.selectedIndex
                    });

                $(this).find("div div:first-child").click(methods.headerClick);
                $(this).find("div div:last-child").toggle();

                if (options.selectedIndex >= 0) {
                    $(this).find("div div:last-child:eq(" + options.selectedIndex + ")").toggle();
                }
            });
        },
        destroy: function () {
        },
        headerClick: function () {
            var accordion = $(this).parent().parent();
            var pane = $(this).parent();
            var data = accordion.data("accordion-data");

            var oldSelectedIndex = data.currentSelectedIndex;
            var currentSelectedIndex = $.inArray(pane.attr("id"), data.options.panels);

            if (oldSelectedIndex != currentSelectedIndex) {
                accordion.find("div div:last-child:visible").toggle('fast');
                pane.find("div:last-child").toggle('fast');
                
                data.currentSelectedIndex = currentSelectedIndex;
            }
            else if (!data.options.requireOpenedPanel) {
                pane.find("div:last-child").toggle('fast');
                data.currentSelectedIndex = -1;
            }
        }
    };

    $.fn.accordion = function (method, options) {
        var settings = $.extend({
            selectedIndex: -1,
            autoSize: autoSize.none,
            panels: null,
            requireOpenedPanel: false
        }, options);

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.accordion');
        }
    };
})(jQuery);